/*
 * Copyright 2015-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* global $, ClipboardJS */

var nextClipboardId = 1;

function multiTransefer(inputText) {
  // 将传入的字符串根据MyBatis的标识拆分成数组
  //删除无关词语jdbc query:
  if(inputText.lastIndexOf('parameters:') > -1 || inputText.lastIndexOf('jdbc query:') > -1 ){
    inputText = inputText.replace('jdbc query:', '');
    //简易压缩sql
    inputText = inputText.split('\n').join('');//删除所有换行
    inputText = inputText.replace(/\s+/g, ' ');//中间多个空格只保留一个
    inputText = inputText.replace(/^\s+/,''); //删除最前面的空格
    return f(inputText);
  } else {
    return inputText;
  }
}

// 单句的问号生成SQL
function f(textVa){
  // 获取带问号的SQL语句
  var statementStartIndex = 0;
  var statementEndIndex;
  if(textVa.lastIndexOf('parameters') > -1){
    statementEndIndex = textVa.indexOf('parameters');
  }else{
    statementEndIndex = textVa.indexOf('[');
  }

  var statementStr = textVa.substring(statementStartIndex, statementEndIndex);
  //获取参数
  var parametersStartIndex = textVa.indexOf('[')+1;
  var parametersEndIndex = textVa.indexOf(']');

  var parametersStr = textVa.substring(parametersStartIndex, parametersEndIndex);
  parametersStr = parametersStr.split(', ');
  // console.log(parametersStr);
  for(var i = 0; i < parametersStr.length; i++) {
    statementStr = statementStr.replace('?', parametersStr[i] + ' ');
  }
  return statementStr;
}

// this is not angular service because it is included in exported traces as well
window.gtClipboard = function (clipboardSelector, clipboardContainer, textFn) {
  setTimeout(function () {
    var $clipboardIcon = $(clipboardSelector);
    if ($clipboardIcon.attr('id')) {
      return;
    }
    var clipboardId = 'gtClipboard' + nextClipboardId++;
    $clipboardIcon.attr('id', clipboardId);
    var clipboardOptions = {
      text: function () {
        return multiTransefer(textFn()) + '\n';
      },
      stopPropagation: true
    };
    var tooltipOptions = {
      title: 'Copy to clipboard',
      placement: 'bottom'
    };
    var $clipboardContainer = $(clipboardContainer);
    clipboardOptions.container = $clipboardContainer[0];
    tooltipOptions.container = $clipboardContainer;
    var clipboard = new ClipboardJS('#' + clipboardId, clipboardOptions);
    $clipboardIcon.tooltip(tooltipOptions);

    clipboard.on('success', function () {
      $clipboardIcon.attr('data-original-title', 'Copied!')
          .tooltip('show');
      setTimeout(function () {
          $clipboardIcon.tooltip('hide');
          $clipboardIcon.attr('data-original-title', 'Copy to clipboard');
        }, 1000);
    });

    $clipboardIcon.data('gtClipboard', true);
  });
};
